#!/usr/bin/env bash
#
# Copyright 2011 Lloyd R. Prentice
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#	 http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# usage zotonic-addsite [site_name]
#

function usage {
    echo "Usage: `basename $0` [options] <site_name>" 1>&2
    echo 1>&2
    echo "  -s <skel>    Skeleton site ('blog' or 'empty'; default: $SKEL)" 1>&2
    echo "  -L           Create site in current directory and symlink it into Zotonic" 1>&2
    echo 1>&2
    echo "  -h <host>    Database host (default: $DBHOST)" 1>&2
    echo "  -p <port>    Database port (default: $DBPORT)" 1>&2
    echo "  -u <user>    Database user (default: $DBUSER)" 1>&2
    echo "  -P <pass>    Database password (default: $DBPASSWORD)" 1>&2
    echo "  -d <name>    Database name (default: $DBDATABASE)" 1>&2
    echo "  -n <schema>  Database schema (default: $DBSCHEMA)" 1>&2
    echo "  -a <pass>    Admin password (default: $ADMINPASSWORD)" 1>&2
    echo 1>&2
    #echo 1>&2
    exit 1
}

#
# Perform the actual adding of the site
#
function do_addsite {
     # Copy the default site as a template
    cp -pR $SKELDIR $SITES/$SITE

    pushd .
    cd $SITES/$SITE

    # Remove placeholder .empty files
    rm `find . -name .empty`

    # move site module in place
    mv SITE.erl $SITE.erl

    # move config in place
    mv config.in config

    # Some handy variables
    if [ `uname -s` = Darwin ]
    then
        FULLNAME=`dscl . -read /Users/$USER RealName | tail -1 | cut -c 2-`
    else
        FULLNAME=`getent passwd "$USER" | cut -d ':' -f 5|cut -d ',' -f 1`
    fi
    YEAR=`date +'%Y'`
    DATE=`date +'%Y-%m-%d'`

    # simple key-value replace
    seds=""
    for var in SITE SKEL FULLNAME DBHOST DBPORT DBUSER DBPASSWORD DBDATABASE DBSCHEMA ADMINPASSWORD YEAR DATE; do
        seds="s/%%$var%%/${!var}/g;$seds"
    done
    sed -i -e "$seds" $SITE.erl config

    popd

    if [ "$DO_LINK" = "1" ]; then
        # symlink site into zotonic
        mv $SITES/$SITE .
        ln -s $PWD/$SITE $SITES/$SITE
    fi

    # Make the new site
    make -C $ZOTONIC

    echo
    echo "Added site $SITE. Now start zotonic and go to http://$SITE:${ZOTONIC_PORT:=8000}/ to view it."
}


#
# Get the options for the addsite command.
#
function check_options {

    if [ -f "$HOME/.zotonic-defaults" ]; then
        source "$HOME/.zotonic-defaults"
    fi

    # The defaults
    SKEL=${SKEL:=blog}
    DBHOST=${DBHOST:=127.0.0.1}
    DBPORT=${DBPORT:=5432}
    DBUSER=${DBUSER:=zotonic}
    DBPASSWORD=${DBPASSWORD:=zotonic}
    DBDATABASE=${DBDATABASE:=zotonic}
    DBSCHEMA=${DBSCHEMA:=public}
    ADMINPASSWORD=${ADMINPASSWORD:=admin}
    DO_LINK=0

    while getopts "s:d:h:p:u:P:n:a:L" optionName; do
        case "$optionName" in
            s) SKEL="$OPTARG";;
            h) DBHOST="$OPTARG";;
            p) DBPORT="$OPTARG";;
            u) DBUSER="$OPTARG";;
            P) DBPASSWORD="$OPTARG";;
            d) DBDATABASE="$OPTARG";;
            n) DBSCHEMA="$OPTARG";;
            a) ADMINPASSWORD="$OPTARG";;
            L) DO_LINK=1;;
            [?]) exit
        esac

    done

    SITE=${!OPTIND}

    if [ "$SITE" == "" ]; then
        usage
    fi

    # allow %%SITE%% syntax in db name and schema
    DBDATABASE=`echo $DBDATABASE|sed "s/%%SITE%%/$SITE/g"`
    DBSCHEMA=`echo $DBSCHEMA|sed "s/%%SITE%%/$SITE/g"`

    if [ -e $SITES/$SITE ]; then
        echo `basename $0`: cannot add site "\`$SITE'": $SITES/$SITE already exists 1>&2
        exit 1
    fi

    SKELDIR="$ZOTONIC/priv/skel/$SKEL"
    if [ ! -e $SKELDIR ]; then
        echo `basename $0`: skeleton site $SKEL does not exists 1>&2
        exit 1
    fi

    echo 1>&2
    echo "==== Add site ====" 1>&2
    echo 1>&2
    echo " Site name: $SITE" 1>&2
    echo " Site directory: $SITES/$SITE" 1>&2
    echo " Skeleton site: $SKEL" 1>&2
    echo 1>&2
    echo " Database host: $DBHOST" 1>&2
    echo " Database port: $DBPORT" 1>&2
    echo " Database user: $DBUSER" 1>&2
    echo " Database password: $DBPASSWORD" 1>&2
    echo " Database name: $DBDATABASE" 1>&2
    echo " Database schema: $DBSCHEMA" 1>&2
    echo " Admin password: $ADMINPASSWORD" 1>&2
    echo 1>&2

    read -p '>>> Hit return to proceed...'
}


check_options $*
do_addsite

